﻿using System;
using System.Collections.Generic;
using System.Data.OracleClient;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using xfsale.lib;

namespace xfsale.ui
{
    public partial class FrmBase : UserControl
    {
        protected static SortedList<string, SysDict> dictSet = App.DictSet;
        protected static SortedList<string, SysMenu> menuSet = App.MenuSet;
        protected static SortedList<string, SysProc> procSet = App.ProcSet;
        protected static SysOp op = App.Op;
        protected static SysInfo sys = App.Sys;
        protected static OracleConnection conn = App.Conn;
        protected SysMenu menu = null;
        protected SysProc proc = null;
        protected string returnCode = null;
        protected string returnMsg = null;

        public FrmBase()
        {
            InitializeComponent();
        }

        protected bool InputPassword(out string password)
        {
            /*WinPassword frmPassword = new WinPassword();
            DialogResult result = frmPassword.ShowDialog();
            if (result == DialogResult.Cancel)
            {
                frmPassword.Dispose();
                password = null;
                return false;
            }

            password = frmPassword.Password;
            frmPassword.Dispose();*/
            password = "123456";
            return true;
        }

        protected void InitCtrl(Control ctrl, string itemName)
        {
            _InitCtrl(ctrl, itemName, true);
        }

        protected void ResetCtrl(Control ctrl, string itemName)
        {
            _InitCtrl(ctrl, itemName, false);
        }

        protected void InitFrame()
        {
            _InitCtrl(this);
        }

        protected void ResetFrame()
        {
            _ResetCtrl(this);
        }

        protected void CloseFrame()
        {
            //(ParentForm.Controls["Splitter"] as DevExpress.XtraEditors.SplitContainerControl).Panel1.Enabled = true;
            //(ParentForm.Controls["NavPanel"] as DevExpress.XtraEditors.PanelControl).Enabled = true;
            this.Dispose();
        }

        protected void SetProcParamIn(string itemName, string paramValue)
        {
            if (proc == null)
                return;

            proc.SetParam("pi_" + itemName, paramValue);
        }

        protected string GetProcParamOut(string itemName)
        {
            if (proc == null)
                return null;

            return proc.GetParam("po_" + itemName);
        }

        protected void BindProcParamsIn()
        {
            _BindProcParamsIn(this);
        }

        protected void BindProcParamsOut()
        {
            _BindProcParamsOut(this);
        }

        protected void CallProc()
        {
            if (proc == null)
                return;

            BindProcParamsIn();

            //FillParam();
            SetProcParamIn("ServiceCode", menu.ServiceCode);
            SetProcParamIn("RegionCode", op.RegionCode);
            SetProcParamIn("BranchCode", op.BranchCode);
            SetProcParamIn("NetCode", op.NetCode);
            SetProcParamIn("OpCode", op.OpCode);
            App.CallProc(proc);
            returnCode = GetProcParamOut("ReturnCode");
            returnMsg = GetProcParamOut("ReturnMsg");

            BindProcParamsOut();
        }

        private void FrmBase_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                menu = Tag as SysMenu;
                proc = App.GetProc(menu.CallProc);
                _InitProc();
                //FillCmbBox();
            }
        }

        protected string GetCtrlVal(Control ctrl)
        {
            string value = null;
            if (ctrl is TextBox)
            {
                value = (ctrl as TextBox).Text;
            }
            else if (ctrl is ComboBox)
            {
                if ((ctrl as ComboBox).SelectedItem is ValueTextPair)
                {
                    value = ((ctrl as ComboBox).SelectedItem as ValueTextPair).Value;
                }
            }

            if (string.IsNullOrEmpty(value))
            {
                string itemName = Convert.ToString(ctrl.Tag);
                if (!string.IsNullOrEmpty(itemName))
                {
                    if (!dictSet.ContainsKey(itemName))
                    {
                        Log.ThrowError("数据项不在数据字典中. itemName={0}", itemName);
                    }

                    value = dictSet[itemName].Default;
                }
            }

            return value;
        }

        protected void SetCtrlVal(Control ctrl, string value)
        {
            if (ctrl is TextBox)
            {
                (ctrl as TextBox).Text = value;
            }
            else if (ctrl is ComboBox)
            {
                foreach (object item in (ctrl as ComboBox).Items)
                {
                    if (item is ValueTextPair)
                    {
                        if ((item as ValueTextPair).Value == value)
                        {
                            (ctrl as ComboBox).SelectedItem = item;
                            break;
                        }
                    }
                }
            }
        }

        protected override bool ProcessCmdKey(ref   Message msg, Keys keyData)
        {
            if (!(ActiveControl is Button) && !(ActiveControl is DevExpress.XtraEditors.BaseButton))
            {
                //if (keyData == Keys.Enter || keyData == Keys.Down)
                if (keyData == Keys.Enter)
                {
                    SendKeys.Send("{TAB}");
                    return true;
                }

                /*if (keyData == Keys.Up)
                {
                    SendKeys.Send("+{Tab}");
                    return true;
                }*/
            }

            return base.ProcessCmdKey(ref   msg, keyData);
        }

        private void _InitCtrl(Control ctrl, string itemName, bool rebuild)
        {
            bool bind = false;
            string ctrlValue = string.Empty;
            if (!string.IsNullOrEmpty(itemName))
            {
                if (!dictSet.ContainsKey(itemName))
                {
                    Log.ThrowError("数据项不在字典中. itemName={0}", itemName);
                }

                bind = true;
                ctrlValue = dictSet[itemName].Default;
            }

            if (ctrl is TextBox)
            {
                TextBox txt = ctrl as TextBox;
                txt.Text = ctrlValue;
            }

            if (ctrl is ComboBox)
            {
                if (bind)
                {
                    ComboBox cmb = ctrl as ComboBox;
                    if (rebuild)
                    {
                        cmb.Items.Clear();
                        cmb.DisplayMember = "Text";
                        cmb.ValueMember = "Value";
                        foreach (SysDictValue item in dictSet[itemName].Values)
                        {
                            ValueTextPair pair = new ValueTextPair();
                            pair.Value = item.Value;
                            pair.Text = item.Value + " " + item.Desc;
                            cmb.Items.Add(pair);
                            if (item.Value == ctrlValue)
                            {
                                cmb.SelectedItem = pair;
                            }
                        }
                    }
                    else
                    {
                        foreach (ValueTextPair pair in cmb.Items)
                        {
                            if (pair.Value == ctrlValue)
                            {
                                cmb.SelectedItem = pair;
                            }
                        }
                    }
                }
            }
        }

        private void _InitCtrl(Control ctrl)
        {
            foreach (Control c in ctrl.Controls)
            {
                if (c.HasChildren)
                {
                    _InitCtrl(c);
                    continue;
                }

                InitCtrl(c, Convert.ToString(c.Tag));
            }
        }

        private void _ResetCtrl(Control ctrl)
        {
            foreach (Control c in ctrl.Controls)
            {
                if (c.HasChildren)
                {
                    _ResetCtrl(c);
                    continue;
                }

                ResetCtrl(c, Convert.ToString(c.Tag));
            }
        }

        private void _InitProc()
        {
            if (proc == null)
                return;

            foreach (SysProcParam param in proc.Params.Values)
            {
                string itemName = param.Name;
                if (param.Length <= 0)
                {
                    if (itemName.StartsWith("PI_"))
                    {
                        itemName = itemName.Substring(3);
                        if (dictSet.ContainsKey(itemName))
                        {
                            param.Value = dictSet[itemName].Default;
                        }
                    }
                    if (itemName.StartsWith("PO_"))
                    {
                        itemName = itemName.Substring(3);
                        if (itemName == "RETURNCODE")
                            param.Length = 4;
                        if (itemName == "RETURNMSG")
                            param.Length = 100;
                    }
                    /*else if (itemName.StartsWith("PO_"))
                    {
                        itemName = param.Name.Substring(3);
                        if (_dict.ContainsKey(itemName))
                        {
                            param.Length = _dict[itemName].Scale;
                        }
                    }*/
                }
            }

        }

        private void _BindProcParamsIn(Control ctrl)
        {
            if (proc == null)
                return;

            foreach (Control c in ctrl.Controls)
            {
                if (c.HasChildren)
                {
                    _BindProcParamsIn(c);
                    continue;
                }

                string itemName = Convert.ToString(c.Tag);
                if (!string.IsNullOrEmpty(itemName))
                {
                    if (proc.Params.ContainsKey("pi_" + itemName))
                    {
                        SetProcParamIn(itemName, GetCtrlVal(c));
                    }
                }
            }
        }

        private void _BindProcParamsOut(Control ctrl)
        {
            if (proc == null)
                return;

            foreach (Control c in ctrl.Controls)
            {
                if (c.HasChildren)
                {
                    _BindProcParamsOut(c);
                    continue;
                }

                string itemName = Convert.ToString(c.Tag);
                if (!string.IsNullOrEmpty(itemName))
                {
                    if (proc.Params.ContainsKey("po_" + itemName))
                    {
                        SetCtrlVal(c, GetProcParamOut(itemName));
                    }
                }
            }
        }
    }
}
